package server;

import register.ServiceRegister;
import register.ZkServiceRegister;

import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Map;

public class ServiceProvider {
    private Map<String, Object> interfaceProvider;
    private ServiceRegister serviceRegister;
    private String ip;
    private Integer port;

    public ServiceProvider(String ip, Integer port){
        this.ip = ip;
        this.port = port;
        interfaceProvider = new HashMap<>();
        serviceRegister = new ZkServiceRegister();
    }

    public void addService(Object service){
        Class<?>[] interfaces = service.getClass().getInterfaces();
        for (Class<?> aClass : interfaces) {
            interfaceProvider.put(aClass.getName(), service);
            serviceRegister.register(aClass.getName(), new InetSocketAddress(ip, port));
        }
    }

    public Object getService(String interfaceName){
        return interfaceProvider.get(interfaceName);
    }
}
